<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qqmlpropertymap.cpp -->
  <title>QQmlPropertyMap  类 | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-module.html">C++ 类</a></td><td >QQmlPropertyMap</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#public-functions">公用函数</a></li>
<li class="level1"><a href="#signals">信号</a></li>
<li class="level1"><a href="#static-public-members">静态公用成员</a></li>
<li class="level1"><a href="#protected-functions">保护的函数</a></li>
<li class="level1"><a href="#details">详细信息</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QQmlPropertyMap 类</h1>
<!-- $$$QQmlPropertyMap-brief -->
<p>
<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">
QQmlPropertyMap</a>类允许您设置可以在QML绑定使用的键-值对。 <a href="#details">更多...</a></p>
<!-- @@@QQmlPropertyMap -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 头文件：</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QQmlPropertyMap&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr><tr><td class="memItemLeft rightAlign topAlign">继承自：</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr></table></div><ul>
<li><a href="qqmlpropertymap-members.html"></a></li>
<li><a href="qqmlpropertymap-obsolete.html">废弃的成员</a></li>
</ul>
<a name="public-functions"></a>
<h2 id="public-functions">公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#QQmlPropertyMap">QQmlPropertyMap</a></b>(QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#dtor.QQmlPropertyMap">~QQmlPropertyMap</a></b>() override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#clear">clear</a></b>(const QString &amp;<i>key</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#contains">contains</a></b>(const QString &amp;<i>key</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#count">count</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#insert">insert</a></b>(const QString &amp;<i>key</i>, const QVariant &amp;<i>value</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#isEmpty">isEmpty</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QStringList </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#keys">keys</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#size">size</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#value">value</a></b>(const QString &amp;<i>key</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant &amp;</td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#operator-5b-5d">operator[]</a></b>(const QString &amp;<i>key</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#operator-5b-5d-1">operator[]</a></b>(const QString &amp;<i>key</i>) const</td></tr>
</table></div>
<ul>
<li class="fn">31 个公用函数继承自<a href="../qtcore/qobject.html#public-functions">QObject</a></li>
</ul>
<a name="signals"></a>
<h2 id="signals">信号</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#valueChanged">valueChanged</a></b>(const QString &amp;<i>key</i>, const QVariant &amp;<i>value</i>)</td></tr>
</table></div>
<ul>
<li class="fn">2 个信号继承自<a href="../qtcore/qobject.html#signals">QObject</a></li>
</ul>
<a name="static-public-members"></a>
<h2 id="static-public-members">静态公用成员</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#staticMetaObject-var">staticMetaObject</a></b></td></tr>
</table></div>
<ul>
<li class="fn">9 个静态公用成员继承自<a href="../qtcore/qobject.html#static-public-members">QObject</a></li>
</ul>
<a name="protected-functions"></a>
<h2 id="protected-functions">保护的函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#QQmlPropertyMap-1">QQmlPropertyMap</a></b>(DerivedType *<i>derived</i>, QObject *<i>parent</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlpropertymap.html#updateValue">updateValue</a></b>(const QString &amp;<i>key</i>, const QVariant &amp;<i>input</i>)</td></tr>
</table></div>
<ul>
<li class="fn">9 个保护函数继承自<a href="../qtcore/qobject.html#protected-functions">QObject</a></li>
</ul>
<h3>额外的继承成员</h3>
<ul>
<li class="fn">1 个属性继承自<a href="../qtcore/qobject.html#properties">QObject</a></li>
<li class="fn">1 个公用槽函数继承自<a href="../qtcore/qobject.html#public-slots">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QQmlPropertyMap-description -->
<div class="descr">
<h2 id="details">详细信息</h2>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">QQmlPropertyMap</a>类让你可以在QML绑定使用设置键-值对。</p>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">QQmlPropertyMap</a>提供了一种向 UI 层公开域数据的便捷方式。以下示例显示了如何在 C++ 中声明数据，然后在 QML 中访问它。</p>
<p>在 C++ 文件中：</p>
<pre class="cpp">

  <span class="comment">// create our data</span>
  <span class="type"><a href="qqmlpropertymap.html#QQmlPropertyMap">QQmlPropertyMap</a></span> ownerData;
  ownerData<span class="operator">.</span>insert(<span class="string">&quot;name&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;John Smith&quot;</span>)));
  ownerData<span class="operator">.</span>insert(<span class="string">&quot;phone&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span>(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;555-5555&quot;</span>)));

  <span class="comment">// expose it to the UI layer</span>
  <span class="type"><a href="../qtquick/qquickview.html">QQuickView</a></span> view;
  <span class="type"><a href="qqmlcontext.html">QQmlContext</a></span> <span class="operator">*</span>ctxt <span class="operator">=</span> view<span class="operator">.</span>rootContext();
  ctxt<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;owner&quot;</span><span class="operator">,</span> <span class="operator">&amp;</span>ownerData);

  view<span class="operator">.</span>setSource(<span class="type"><a href="../qtcore/qurl.html">QUrl</a></span><span class="operator">::</span>fromLocalFile(<span class="string">&quot;main.qml&quot;</span>));
  view<span class="operator">.</span>show();

</pre>
<p>然后，在<code>main.qml</code>：</p>
<pre class="cpp">

  Text { text: owner<span class="operator">.</span>name <span class="operator">+</span> <span class="string">&quot; &quot;</span> <span class="operator">+</span> owner<span class="operator">.</span>phone }

</pre>
<p>绑定是动态的 - 每当更新键的值时，绑定到该键的任何内容也将更新。</p>
<p>要检测 UI 层中所做的值更改，您可以连接到<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#valueChanged">valueChanged</a> () 信号。但是，请注意，通过调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#insert">insert</a> () 或<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#clear">clear</a> ()进行更改时<b>不会</b>发出<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#valueChanged">valueChanged</a> () - 它仅在从 QML 更新值时发出。</p>
<p><b>注意：</b>无法从<span lang="zh-cn">map</span>中删除<span lang="zh-cn">键</span>；添加键后，您只能修改或清除其关联值。</p><p><b>注意：</b>从<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">QQmlPropertyMap</a>派生类时，请使用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#QQmlPropertyMap-1">受保护的双参数构造函数</a>，以确保该类已正确注册到 Qt<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/metaobjects.html">元对象系统</a>。</p></div>
<!-- @@@QQmlPropertyMap -->
<div class="func">
<h2>成员函数文档</h2>
<!-- $$$QQmlPropertyMap[overload1]$$$QQmlPropertyMapQObject* -->
<h3 class="fn" id="QQmlPropertyMap"><a name="QQmlPropertyMap"></a>QQmlPropertyMap::<span class="name">QQmlPropertyMap</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>构造一个具有父对象<i>parent</i>的可绑定映射。</p>
<!-- @@@QQmlPropertyMap -->
<!-- $$$QQmlPropertyMap$$$QQmlPropertyMapDerivedType*QObject* -->
<h3 class="fn" id="QQmlPropertyMap-1"><a name="QQmlPropertyMap-1"></a><code>[protected] </code>QQmlPropertyMap::<span class="name">QQmlPropertyMap</span>(<span class="type">DerivedType</span> *<i>derived</i>, <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i>)</h3>
<p>构造一个具有父对象<i>parent</i>的可绑定映射。在从<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">QQmlPropertyMap</a>派生的类中使用此构造<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">函数</a>。</p>
<p><i>派生</i>的类型用于向<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/metaobjects.html">元对象系统</a>注册属性映射，这是确保派生类的属性可访问所必需的。此类型必须从<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html">QQmlPropertyMap</a>派生。</p>
<!-- @@@QQmlPropertyMap -->
<!-- $$$~QQmlPropertyMap[overload1]$$$~QQmlPropertyMap -->
<h3 class="fn" id="dtor.QQmlPropertyMap"><a name="dtor.QQmlPropertyMap"></a><code>[override virtual] </code>QQmlPropertyMap::<span class="name">~QQmlPropertyMap</span>()</h3>
<p>销毁可绑定的<span lang="zh-cn">map</span>。</p>
<!-- @@@~QQmlPropertyMap -->
<!-- $$$clear[overload1]$$$clearconstQString& -->
<h3 class="fn" id="clear"><a name="clear"></a><span class="type">void</span> QQmlPropertyMap::<span class="name">clear</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>)</h3>
<p>清除与<i>key</i>关联的值（如果有）。</p>
<!-- @@@clear -->
<!-- $$$contains[overload1]$$$containsconstQString& -->
<h3 class="fn" id="contains"><a name="contains"></a><span class="type">bool</span> QQmlPropertyMap::<span class="name">contains</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>) const</h3>
<p>如果<span lang="zh-cn">map</span>包含<i>key ，</i>则返回 true 。</p>
<p><b>请参考 </b><a href="qqmlpropertymap.html#size">size</a>()<span lang="zh-cn">。</span></p>
<!-- @@@contains -->
<!-- $$$count[overload1]$$$count -->
<h3 class="fn" id="count"><a name="count"></a><span class="type">int</span> QQmlPropertyMap::<span class="name">count</span>() const</h3>
<p>这是一个重载函数。</p>
<p><span lang="zh-cn">与</span> <a href="qqmlpropertymap.html#size">size</a>()<span lang="zh-cn">相同。</span></p>
<!-- @@@count -->
<!-- $$$insert[overload1]$$$insertconstQString&constQVariant& -->
<h3 class="fn" id="insert"><a name="insert"></a><span class="type">void</span> QQmlPropertyMap::<span class="name">insert</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>, const <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> &amp;<i>value</i>)</h3>
<p>将与<i>key</i>关联的值设置为<i>value</i>。</p>
<p>如果密钥不存在，则会自动创建。</p>
<!-- @@@insert -->
<!-- $$$isEmpty[overload1]$$$isEmpty -->
<h3 class="fn" id="isEmpty"><a name="isEmpty"></a><span class="type">bool</span> QQmlPropertyMap::<span class="name">isEmpty</span>() const</h3>
<p>如果地图不包含键，则返回 true；否则返回假。</p>
<p><b>请参考 </b><a href="qqmlpropertymap.html#size">size</a>()<span lang="zh-cn">。</span></p>
<!-- @@@isEmpty -->
<!-- $$$keys[overload1]$$$keys -->
<h3 class="fn" id="keys"><a name="keys"></a><span class="type"><a href="../qtcore/qstringlist.html">QStringList</a></span> QQmlPropertyMap::<span class="name">keys</span>() const</h3>
<p>返回键列表。</p>
<p>已清除的键仍将出现在此列表中，即使它们的关联值是无效的 QVariants。</p>
<p><b>注意：</b>该函数可以通过元对象系统和 QML 调用。见<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qobject.html#Q_INVOKABLE">Q_INVOKABLE</a>。</p>
<!-- @@@keys -->
<!-- $$$size[overload1]$$$size -->
<h3 class="fn" id="size"><a name="size"></a><span class="type">int</span> QQmlPropertyMap::<span class="name">size</span>() const</h3>
<p>返回映射中的键数。</p>
<p><b>请参考 </b><a href="qqmlpropertymap.html#isEmpty">isEmpty</a>()
<span lang="zh-cn">和 </span><a href="qqmlpropertymap.html#count">count</a>()<span lang="zh-cn">。</span></p>
<!-- @@@size -->
<!-- $$$updateValue[overload1]$$$updateValueconstQString&constQVariant& -->
<h3 class="fn" id="updateValue"><a name="updateValue"></a><code>[virtual protected] </code><span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> QQmlPropertyMap::<span class="name">updateValue</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>, const <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> &amp;<i>input</i>)</h3>
<p>返回要为键<i>key</i>存储的新值。提供此函数是为了从 QML 拦截对属性的更新，其中 QML 提供的值为<i>input</i>。</p>
<p>重写此函数以在更新时操作属性值。请注意，此函数仅在从 QML 更新值时调用。</p>
<!-- @@@updateValue -->
<!-- $$$value[overload1]$$$valueconstQString& -->
<h3 class="fn" id="value"><a name="value"></a><span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> QQmlPropertyMap::<span class="name">value</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>) const</h3>
<p>返回与<i>key</i>关联的值。</p>
<p>如果没有为该键设置值（或者如果该值已被清除），则返回一个无效的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qvariant.html">QVariant</a>。</p>
<!-- @@@value -->
<!-- $$$valueChanged[overload1]$$$valueChangedconstQString&constQVariant& -->
<h3 class="fn" id="valueChanged"><a name="valueChanged"></a><code>[signal] </code><span class="type">void</span> QQmlPropertyMap::<span class="name">valueChanged</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>, const <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> &amp;<i>value</i>)</h3>
<p>每当映射中的一个值发生更改时，就会发出此信号。<i>key</i>是与更改的<i>值</i>对应的键。</p>
<p><b>注意：</b> valueChanged()在通过调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#insert">insert</a> () 或<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlpropertymap.html#clear">clear</a> ()进行更改时<b>不会</b>发出- 只有在从 QML 更新值时才会发出。</p><!-- @@@valueChanged -->
<!-- $$$operator[][overload1]$$$operator[]constQString& -->
<h3 class="fn" id="operator-5b-5d"><a name="operator-5b-5d"></a><span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> &amp;QQmlPropertyMap::<span class="name">operator[]</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>)</h3>
<p>返回与该键相关联的<i><span lang="zh-cn">key</span></i>作为可修改的参考。</p>
<p>如果映射不包含键为 key 的<i>项</i>，则该函数将一个无效的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qvariant.html">QVariant</a>插入键为<i>key</i>的映射中，并返回对它的引用。</p>
<p><b>请参考 </b><a href="qqmlpropertymap.html#insert">insert</a>()
<span lang="zh-cn">和 </span><a href="qqmlpropertymap.html#value">value</a>()<span lang="zh-cn">。</span></p>
<!-- @@@operator[] -->
<!-- $$$operator[]$$$operator[]constQString& -->
<h3 class="fn" id="operator-5b-5d-1"><a name="operator-5b-5d-1"></a><span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> QQmlPropertyMap::<span class="name">operator[]</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>key</i>) const</h3>
<p>这是一个重载函数。</p>
<p><span lang="zh-cn">与 </span><a href="qqmlpropertymap.html#value">value</a>()<span lang="zh-cn">相同。</span></p>
<!-- @@@operator[] -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
